package org.nimi317.web_gis.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import org.nimi317.web_gis.form.post.PermissionPost;
import org.nimi317.web_gis.form.put.PermissionPut;
import org.nimi317.web_gis.entity.Permission;
import org.nimi317.web_gis.entity.R;
import org.nimi317.web_gis.service.IPermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author thunderobot
 */
@RestController
@RequestMapping("/permissions")
@SaCheckRole("admin")
public class PermissionController {
    private final IPermissionService permissionService;

    public PermissionController(IPermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @GetMapping
    public R<?> getAllPermissions() {
        return R.ok(permissionService.findAllPermission());
    }

    @PostMapping
    public R<?> addPermission(@RequestBody @Validated PermissionPost permissionPost) {
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionPost, permission);
        permissionService.addPermission(permission);
        return R.ok("增加成功");
    }


    @PutMapping
    public R<?> updatePermission(@RequestBody @Validated PermissionPut permissionPut) {
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionPut, permission);
        permissionService.updatePermission(permission);
        return R.ok("更新成功");
    }

    @DeleteMapping("{id}")
    public R<?> deletePermission(@PathVariable Integer id) {
        permissionService.deletePermission(id);
        return R.ok("删除成功");
    }

}
